package com.hongkzh.www.friend.controller;

import com.hongkzh.www.conmon.FileUtils;
import com.hongkzh.www.conmon.ReqResponse;
import com.hongkzh.www.friend.service.FriendService;
import com.hongkzh.www.swagger.annotation.*;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author martins
 */
@Api(tag = "friend", description = "乐友接口")
public class FriendController extends Controller {
    /**
     * 上传文件基础虚拟路径
     */
    public static final String USERFILES_BASE_URL = "/userfiles/1//hkMediaCategoryCircle/";
    static FriendService friendService=new FriendService();

    @ApiOperation(url = "/v1/friend/friendList", tag = "friend", httpMethod = "post",description = "通过用户id 返回好友列表")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = " letter 字母 ,name 名称 level 等级 headImg  头像uid 用户id token 聊天token type 类型  1自媒体 2企业广告3城市广告4传统文化 sex 1男2女"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void friendList(){
        String id=getPara("loginUid");
        ReqResponse<Map<String,List<Record>>> r=new ReqResponse();
        Map<String,List<Record>> friends=friendService.friendList(id);


        r.setData(friends);
        renderJson(r);
    }
    @ApiOperation(url = "/v1/friend/addFriend", tag = "friend", httpMethod = "post",description = "通过用户id 增加好友")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "friendId", description = "好友id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "  "),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void addFriend(){
        String id=getPara("loginUid");
        String friendId=getPara("friendId");
        ReqResponse<String> r=new ReqResponse();
        friendService.addFriend(id,friendId);
        r.setData("");
        renderJson(r);
    }
    @ApiOperation(url = "/v1/friend/removeFriend", tag = "friend", httpMethod = "post",description = "通过用户id 删除好友")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "friendId", description = "好友id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "  "),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void removeFriend(){
        String id=getPara("loginUid");
        String friendId=getPara("friendId");
        ReqResponse<String> r=new ReqResponse();
        friendService.removeFriend(id,friendId);
        r.setData("");
        renderJson(r);
    }
    @ApiOperation(url = "/v1/friend/circleList", tag = "friend", httpMethod = "post",description = "通过用户id 返回圈子列表")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = " letter 字母  coverImgSrc 图片 circleId 圈子id circleName 圈子名称categoryName 圈子分类 userCount圈子人数"),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    public void circleList(){
        String id=getPara("loginUid");
        ReqResponse< Map<String,List<Record>> > r=new ReqResponse();
        Map<String,List<Record>>  friends=friendService.circleList(id);
        r.setData(friends);
        renderJson(r);
    }



    @ApiOperation(url = "/v1/friend/createGroup", tag = "friend", httpMethod = "post",description = "通过用户id 创建圈子")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "categoryId", description = "分类id", required = true, dataType = "string"),
            @Param(name = "circleName", description = "圈子名称", required = true, dataType = "string"),
            @Param(name = "coverImgSrc", description = "圈子图片", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "  "),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void createGroup(){
        UploadFile uf= getFile("coverImgSrc");
        String uid=getPara("loginUid");
        String categoryId=getPara("categoryId");
        String circleName=getPara("circleName");
        String coverImgSrc= USERFILES_BASE_URL + uf.getFileName();
        ReqResponse<String> r=new ReqResponse();
        File file = new File(PropKit.get("userfiles.basedir") +coverImgSrc);
        try {
            FileUtils.copyFileUsingFileChannels(uf.getFile(),file);

            friendService.createGroup(uid,categoryId,circleName,coverImgSrc);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);
        }
        r.setData("");
        renderJson(r);
    }
    @ApiOperation(url = "/v1/friend/joinGroup", tag = "friend", httpMethod = "post",description = "通过用户id 加入圈子")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "circleId", description = "圈子Id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "  "),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void joinGroup(){

        String uid=getPara("loginUid");
        String circleId=getPara("circleId");

        ReqResponse<String> r=new ReqResponse();
        try {


            friendService.joinGroup(uid,circleId);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);
        }
        r.setData("");
        renderJson(r);
    }
    @ApiOperation(url = "/v1/friend/quitGroup", tag = "friend", httpMethod = "post",description = "通过用户id 退圈子")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "circleId", description = "圈子Id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "  "),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void quitGroup(){

        String uid=getPara("loginUid");
        String circleId=getPara("circleId");

        ReqResponse<String> r=new ReqResponse();
        try {
            friendService.quitGroup(uid,circleId);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);
        }
        r.setData("");
        renderJson(r);
    }
    @ApiOperation(url = "/v1/friend/dismissGroup", tag = "friend", httpMethod = "post",description = "通过用户id 解除圈子")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "circleId", description = "圈子Id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code=200,message="成功",responseHeaders={
                    @ResponseHeader(name="code",description = " 0成功 1失败"),
                    @ResponseHeader(name="data",description = "  "),
                    @ResponseHeader(name="msg",description = "失败原因") })

    })
    @Before(Tx.class)
    public void dismissGroup(){

        String uid=getPara("loginUid");
        String circleId=getPara("circleId");

        ReqResponse<String> r=new ReqResponse();
        try {
            String re=friendService.dismissGroup(uid,circleId);
            if("不是圈主不能解除圈子".equals(re)){
                r.setCode(1);
                r.setMsg(re);
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(1);
        }
        r.setData("");
        renderJson(r);
    }
}
